<?php
namespace Qy {
    /**
    * 企业微信异步任务接口类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3
    */
    class Batch {
        private $common;
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 增量更新成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function syncUserBatch($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['media_id'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/batch/syncuser?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 全量覆盖成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function replaceUserBatch($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['media_id'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 全量覆盖部门
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function replacePartyBatch($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['media_id'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取异步任务结果
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3
        * 
        * @param mixed $jobid
        * @param mixed $a_token
        */
        public function getResultBatch($jobid,$a_token = '') {
            if (empty($jobid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?access_token={$a_token}&jobid={$jobid}";
            return $this->common->httpRequest($url);
        }
    }
}
?>
